//
//  LBZhutiCourseVC.m
//  V-Learn
//
//  Created by Apple on 2019/1/9.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "LBZhutiCourseVC.h"
#import "LBZhutiCourseCollectionViewCell.h"
#import "LBSectionHeaderView.h"
#import "JSDropDownMenu.h"
#import "LBNiuStudentDetailVC.h"
#import "LBCourseOutputDTOListModel.h"
#import "LBZhutiCourseCollectionViewCellTwo.h"
#import "LBCoureseDetailVC.h"
static NSString *ID=@"LBZhutiCourseCollectionViewCell";
static NSString *ID2=@"LBZhutiCourseCollectionViewCellTwo";

@interface LBZhutiCourseVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSString *preUrl;

@end
@implementation LBZhutiCourseVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置滚动方向
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
            layout.itemSize = CGSizeMake(166*kScaleFit, 228*kScaleFit);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-LBNAVBAR_HEIGHT) collectionViewLayout:layout];
        _collectionView.backgroundColor = rgb(242,242,242);
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        
    }
    return _collectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.collectionView registerClass:[LBZhutiCourseCollectionViewCell class] forCellWithReuseIdentifier:@"LBZhutiCourseCollectionViewCell"];
    [self.collectionView registerClass:[LBZhutiCourseCollectionViewCellTwo class] forCellWithReuseIdentifier:@"LBZhutiCourseCollectionViewCellTwo"];
    
    
    [self setupUI];
}

- (void)setupUI{

    //底部背景图
    UIImageView *bgImageView = [[UIImageView alloc] init];
    bgImageView.image = [UIImage imageNamed:@"wk_zhuti_bg"];
    [self.view addSubview:bgImageView];
    [bgImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
    //顶部View
    UIView *topView = [UIView new];
    topView.userInteractionEnabled = YES;
    [self.view addSubview:topView];
    topView.frame = CGRectMake(0, 0, ScreenW, LBNAVBAR_HEIGHT);
    topView.backgroundColor = [UIColor clearColor];
    
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.frame = CGRectMake(17*ScaleX, 30*ScaleX, 25*ScaleX, 25*ScaleX);
    [btn addTarget:self action:@selector(BackAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [btn setImage:[UIImage imageNamed:@"wk_back_white"] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:@"wk_back_white"] forState:(UIControlStateHighlighted)];
    [topView addSubview:btn];
    
    UILabel *titleLab = [UILabel labelWithText:@"主题课程" andTextColor:rgb(255,255,255) andName:nil andFontSize:18*kScaleFit];
    titleLab.text = self.titleString;
    [topView addSubview:titleLab];
    [titleLab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(topView);
        make.top.mas_equalTo(28*kScaleFit);
    }];
    

    
    //添加 collectionView
    [self.view addSubview:self.collectionView];
//    [self.collectionView registerClass:[LBSectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Cell"];
    [self requestZhutiCourseData];
    
}
- (void)BackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 主题课程接口
- (void)requestZhutiCourseData{
    
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setObject:self.cateId forKey:@"cateId"];

    
    [WYHXNetWorkManager get:WKHomemoreCourse_WKURL params:(NSDictionary *)data completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        if (code.integerValue==200) {
//            NSLog(@"%@",responseObj);
            NSArray *arr = [LBCourseOutputDTOListModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
            [self.dataArray addObjectsFromArray:arr];
             [self.collectionView reloadData];
        }
    }];
}
//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row%2==0) {
        return CGSizeMake(166*kScaleFit, 220*kScaleFit);
    }else{
        return CGSizeMake(166*kScaleFit, 238*kScaleFit);
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size = CGSizeMake(SCREEN_WIDTH, 10);
    return size;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%ld区%ld行",indexPath.section,indexPath.row);
    //在这里进行点击cell后的操作
//    LBNiuStudentDetailVC *niuDetailVC = [LBNiuStudentDetailVC new];
//    [self.navigationController pushViewController:niuDetailVC animated:YES];
    LBCourseOutputDTOListModel *model = self.dataArray[indexPath.row];
    LBCoureseDetailVC *courseDetailVC = [[LBCoureseDetailVC alloc] init];
    courseDetailVC.courseId = model.courseId;
    courseDetailVC.type = model.courseType;
    [self.navigationController pushViewController:courseDetailVC animated:YES];
}
- (void)requestProductListData{
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID=@"LBZhutiCourseCollectionViewCell";
    LBZhutiCourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    static NSString *ID2=@"LBZhutiCourseCollectionViewCellTwo";
    LBZhutiCourseCollectionViewCellTwo *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:ID2 forIndexPath:indexPath];
    LBCourseOutputDTOListModel *model = self.dataArray[indexPath.row];
    
    if (indexPath.row%2==0) {
        cell.model = model;
        return cell;
    }else{
        cell2.model = model;
       return cell2;
    }

}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}
@end
